<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:rich="http://richfaces.org/rich"
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:s="http://jboss.org/seam/faces"
      xml:lang="en" lang="en">

<ui:composition template="/resources/layout.xhtml">
    <ui:define name="title" >Займы</ui:define>
    <ui:define name="content">
        <div class="all_loan_list_page">
            <h:form id="addNewLoanForm">
                <div class="for_add_form">
                    <div class="add_form">
                        <table>
                            <tr>
                                <td style="width:110px;"><div><h:outputText value="Сумма займа:" /></div></td>
                                <td><div>
                                    <rich:inputNumberSpinner
                                            id="inputLoanAmount"
                                            value="#{loanAction.loanModel.amount}"
                                            minValue="2000"
                                            maxValue="25000"
                                            step="500"/>
                                </div></td>
                                <td><div><h:message for="inputLoanAmount" /></div></td>
                            </tr>
                            <tr>
                                <td><div><h:outputText value="Срок займа:" /></div></td>
                                <td><div>
                                    <rich:inputNumberSpinner
                                            value="#{loanAction.loanModel.period}"
                                            maxValue="21"
                                            minValue="7"/>
                                </div></td>
                            </tr>
                            <tr>
                                <td><div>Заниматель: </div></td>
                                <td><div>
                                    <rich:autocomplete
                                            style="float: left;"
                                            value="#{loanAction.currentDebtorStr}"
                                            mode="ajax"
                                            autocompleteMethod="#{loanAction.autocomplete}" >
                                    </rich:autocomplete>

                                    <rich:panel styleClass="fake-app" style="float: left;">
                                        <a4j:commandButton value=" "
                                                           styleClass="addDeptorButton"
                                                           oncomplete="#{rich:component('popupAddDebtor')}.show()"
                                                           render="addNewDebtorPanel,addDebtorHeader"
                                                />
                                        <rich:tooltip >
                                            Добавление нового занимателя
                                        </rich:tooltip>
                                    </rich:panel>
                                </div></td>
                            </tr>
                            <tr>
                                <td colspan="2"><div class="addNewLoanButton">
                                    <a4j:commandButton value="Заключить"
                                                       action="#{loanAction.addNewLoan}"
                                                       render="loanList,addNewLoanForm"/>
                                </div>
                                    <div style="clear:both;" />
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>

                <rich:dataTable id="loanList" value="#{loanDAO.loanList}" var="loan" >

                    <rich:column sortBy="#{loan.debtor.FIO}"
                                 sortOrder="#{loanAction.loanListSort.debtorOrder}"
                                 styleClass="#{loan.loanOverdue ? 'overdue' : ''}"
                            >
                        <f:facet name="header">
                            <a4j:commandLink value="Заниматель"
                                             render="loanList"
                                             styleClass="fake-app"
                                             title="Заниматель"
                                             action="#{loanAction.loanListSort.sortByDebtor}" />
                        </f:facet>
                        <h:outputText value="#{loan.debtor.FIO}" />

                        <f:facet name="footer">
                            <div>
                                <div>
                                    <h:outputText value="Итого: "/>
                                </div>
                                <div>
                                    <h:outputText value="Сумма капитала:" />
                                </div>
                                <div>
                                    <h:outputText value="Сумма на руках:"/>
                                </div>
                            </div>
                        </f:facet>
                    </rich:column>

                    <rich:column sortBy="#{loan.amount}"

                                 sortOrder="#{loanAction.loanListSort.amountOrder}"
                                 styleClass="#{loan.loanOverdue ? 'overdue' : ''} number-column">
                        <f:facet name="header">
                            <a4j:commandLink value="Сумма займа"
                                             styleClass="fake-app"
                                             render="loanList"
                                             title="Сумма займа"
                                             action="#{loanAction.loanListSort.sortByAmount}" />
                        </f:facet>
                        <h:outputText value="#{loan.amount}" >
                            <f:convertNumber pattern="###,##0.0" />
                        </h:outputText>
                        <f:facet name="footer">

                            <div class="number-column">
                                <div>
                                    <h:outputText value="#{loanAction.totalLoan.amount}">
                                        <f:convertNumber pattern="###,##0.0" />
                                    </h:outputText>
                                </div>

                                <div>
                                    <h:outputText value="#{userSetting.currentFilial.capital}">
                                        <f:convertNumber pattern="###,##0.0" />
                                    </h:outputText>
                                </div>

                                <div>
                                    <h:outputText value="#{userSetting.currentFilial.capital - loanAction.totalLoan.amount}">
                                        <f:convertNumber pattern="###,##0.0" />
                                    </h:outputText>
                                </div>
                            </div>
                        </f:facet>
                    </rich:column>

                    <rich:column sortBy="#{loan.createDate}"
                                 sortOrder="#{loanAction.loanListSort.createDateOrder}"
                                 styleClass="#{loan.loanOverdue ? 'overdue' : ''} date-column">
                        <f:facet name="header">
                            <a4j:commandLink value="Дата договора"
                                             render="loanList"
                                             styleClass="fake-app"
                                             title="Дата заключения договора"
                                             action="#{loanAction.loanListSort.sortByCreateDate}" />
                        </f:facet>
                        <h:outputText value="#{loan.createDateStr}" />
                    </rich:column>

                    <rich:column sortBy="#{loan.period}"
                                 sortOrder="#{loanAction.loanListSort.periodOrder}"
                                 styleClass="#{loan.loanOverdue ? 'overdue' : ''} number-column">
                        <f:facet name="header">
                            <a4j:commandLink value="Срок займа"
                                             render="loanList"
                                             styleClass="fake-app"
                                             title="Срок займа"
                                             action="#{loanAction.loanListSort.sortByPeriod}" />
                        </f:facet>
                        <h:outputText value="#{loan.period}" styleClass="number" />
                    </rich:column>

                    <rich:column sortBy="#{loan.plannedAmount}"
                                 sortOrder="#{loanAction.loanListSort.plannedAmountOrder}"
                                 styleClass="#{loan.loanOverdue ? 'overdue' : ''} number-column">
                        <f:facet name="header">
                            <a4j:commandLink value="Сумма долга"
                                             styleClass="fake-app"
                                             render="loanList"
                                             title="Расчитанная планируемая сумма долга"
                                             action="#{loanAction.loanListSort.sortByPlannedAmount}" />
                        </f:facet>
                        <h:outputText value="#{loan.plannedAmount}">
                            <f:convertNumber pattern="###,##0.0" />
                        </h:outputText>
                    </rich:column>

                    <rich:column sortBy="#{loan.feeForUseOfFunds}"
                                 sortOrder="#{loanAction.loanListSort.feeForUseOfFundsOrder}"
                                 styleClass="#{loan.loanOverdue ? 'overdue' : ''} number-column">
                        <f:facet name="header">
                            <a4j:commandLink value="Плата за п.д.с."
                                             styleClass="fake-app"
                                             render="loanList"
                                             action="#{loanAction.loanListSort.sortByFeeForUseOfFunds}"
                                             title="Плата за пользование денежными средствами"/>
                        </f:facet>
                        <h:outputText value="#{loan.feeForUseOfFunds}">
                            <f:convertNumber pattern="###,##0.0" />
                        </h:outputText>
                    </rich:column>

                    <rich:column sortBy="#{loan.plannedDate}"
                                 sortOrder="#{loanAction.loanListSort.plannedDateOrder}"
                                 styleClass="#{loan.loanOverdue ? 'overdue' : ''} date-column">
                        <f:facet name="header">
                            <a4j:commandLink value="Дата исполнения обязательств"
                                             render="loanList"
                                             styleClass="fake-app"
                                             title="Дата исполнения обязательств"
                                             action="#{loanAction.loanListSort.sortByPlannedDate}" />
                        </f:facet>
                        <h:outputText value="#{loan.plannedDateStr}" />
                    </rich:column>

                    <rich:column sortBy="#{loan.overdueDays}"
                                 sortOrder="#{loanAction.loanListSort.overdueDaysOrder}"
                                 styleClass="#{loan.loanOverdue ? 'overdue' : ''} number-column">
                        <f:facet name="header">
                            <a4j:commandLink value="Кол-во просроченных дней"
                                             render="loanList"
                                             styleClass="fake-app"
                                             title="Количество просроченных дней"
                                             action="#{loanAction.loanListSort.sortByOverdueDays}" />
                        </f:facet>
                        <h:outputText value="#{loan.overdueDays}" rendered="#{loan.loanOverdue}"/>
                    </rich:column>

                    <rich:column sortBy="#{loan.currentAmount}"
                                 sortOrder="#{loanAction.loanListSort.currentAmountOrder}"
                                 styleClass="#{loan.loanOverdue ? 'overdue' : ''} number-column">
                        <f:facet name="header">
                            <a4j:commandLink value="Текущая сумма долга"
                                             render="loanList"
                                             styleClass="fake-app"
                                             title="Текущая сумма долга с учётом штрафов"
                                             action="#{loanAction.loanListSort.sortByCurrentAmount}" />
                        </f:facet>
                        <h:outputText value="#{loan.currentAmount}">
                            <f:convertNumber pattern="###,##0.0" />
                        </h:outputText>
                    </rich:column>

                    <rich:column sortBy="#{loan.factFeeForUseOfFunds}"
                                 sortOrder="#{loanAction.loanListSort.factFeeForUseOfFundsOrder}"
                                 styleClass="#{loan.loanOverdue ? 'overdue' : ''} number-column">
                        <f:facet name="header">
                                <a4j:commandLink value="Текущая плата за п.д.с."
                                                 styleClass="fake-app"
                                                 render="loanList"
                                                 title="Текущая плата за пользование денежными средствами"
                                                 action="#{loanAction.loanListSort.sortByFactFeeForUseOfFunds}" />
                        </f:facet>
                        <h:outputText value="#{loan.factFeeForUseOfFunds}">
                            <f:convertNumber pattern="###,##0.0" />
                        </h:outputText>
                    </rich:column>

                    <rich:column sortBy="#{loan.owner.filial.name}"
                                 sortOrder="#{loanAction.loanListSort.filialOrder}"
                                 styleClass="#{loan.loanOverdue ? 'overdue' : ''}"
                                 rendered="#{identity.hasRole('admin', 'default', 'default')}"
                            >
                        <f:facet name="header">
                            <a4j:commandLink value="Филиал"
                                             render="loanList"
                                             styleClass="fake-app"
                                             title="Филиал"
                                             action="#{loanAction.loanListSort.sortByFilial}" />
                        </f:facet>
                        <h:outputText value="#{loan.owner.filial.name}" />
                    </rich:column>



                    <rich:column>
                        <div style="width: 50px;">
                            <div style="float: left;">
                                <rich:panel styleClass="fake-app">
                                    <a4j:commandButton value=" "
                                                       action="#{loanAction.setCurrentLoan(loan)}"
                                                       onclick="#{rich:component('popupEditLoan')}.show()"
                                                       render="editLoanPanel"
                                                       styleClass="edit-button" />
                                    <rich:tooltip >
                                        Редактирование информации о займе
                                    </rich:tooltip>
                                </rich:panel>
                            </div>
                            <div style="float: left;">
                                <rich:panel styleClass="fake-app">
                                    <a4j:commandButton value=" " styleClass="delete-button"
                                                       action="#{loanAction.removeLoan(loan)}"
                                                       render="loanList"/>
                                    <rich:tooltip>
                                        Удаление займа
                                    </rich:tooltip>
                                </rich:panel>
                            </div>
                            <div style="float: left;">
                                <rich:panel styleClass="fake-app">
                                    <a4j:commandButton value=" " styleClass="finish-button"
                                                       render="loanList"
                                                       action="#{loanAction.finishLoan(loan)}"/>
                                    <rich:tooltip id="finish_button_tooltip" layout="block" styleClass="tooltip" >
                                        Завершение договора о займе
                                    </rich:tooltip>
                                </rich:panel>
                            </div>
                            <div style="clear: both;"/>
                        </div>
                    </rich:column>

                </rich:dataTable>
            </h:form>
        </div>


        <ui:include src="popup/addNewDebtor.xhtml">
            <ui:param name="headerValue" value="Добавление нового занимателя"/>
            <ui:param name="debtorModel" value="#{debtorAction.debtorModel}"/>
        </ui:include>


        <rich:popupPanel id="popupEditLoan" modal="true" autosized="true">
            <f:facet name="header">
                <h:outputText value="Редактирование информации о займе"/>
            </f:facet>
            <f:facet name="controls">
                <h:outputLink value="#" onclick="hidePopupEditLoan();" >
                    X
                </h:outputLink>

                <a4j:jsFunction name="hidePopupEditLoan"
                                oncomplete="#{rich:component('popupEditLoan')}.hide(); return false;"/>
            </f:facet>
            <h:form id="editLoanForm" >
                <h:panelGroup id="editLoanPanel" class="all_loan_list_page">
                <div class="for_add_form">
                    <div class="add_form">

                        <h:inputHidden value="#{loanAction.currentLoan.contractId}"/>
                        <table>
                            <tr>
                                <td><div><h:outputText value="Дата заключения договора"/></div></td>
                                <td><div><rich:calendar
                                        enableManualInput="true"
                                        datePattern="dd.MM.yyyy"
                                        value="#{loanAction.currentLoan.createDate}"/>
                                </div></td>
                            </tr>
                            <tr>
                                <td style="width:110px;"><div><h:outputText value="Сумма займа:" /></div></td>
                                <td><div>
                                    <rich:inputNumberSpinner
                                            id="inputCurrentLoanAmount"
                                            value="#{loanAction.currentLoan.amount}"
                                            minValue="1000"
                                            maxValue="25000"
                                            step="500"/>
                                </div></td>
                            </tr>
                            <tr>
                                <td><div><h:outputText value="Срок займа:" /></div></td>
                                <td><div>
                                    <rich:inputNumberSpinner
                                            value="#{loanAction.currentLoan.period}"
                                            maxValue="14"
                                            minValue="1"/>
                                </div></td>
                            </tr>

                            <tr>
                                <td>
                                    <div>Заниматель:</div>
                                </td>
                                <td>
                                    <div>
                                        <rich:autocomplete
                                                id="debtor"
                                                value="#{loanAction.currentDebtorStr}"
                                                mode="ajax"
                                                autocompleteMethod="#{loanAction.autocomplete}">
                                        </rich:autocomplete>
                                        <h:commandButton value=" " styleClass="addDeptorButton">
                                            <rich:componentControl target="popupAddDebtor" operation="show"/>
                                        </h:commandButton>
                                    </div>
                                </td>
                            </tr>

                            <tr>
                                <td colspan="2"><div class="addNewLoanButton">
                                    <a4j:commandButton id="updateCurrentLoanButton"
                                            value="Сохранить"
                                            oncomplete="if (#{!loanAction.hasError}) {
                                                hidePopupEditLoan();
                                            }"
                                            action="#{loanAction.updateCurrentLoan}"
                                            render="loanList"
                                            />
                                </div>
                                    <div style="clear:both;" />
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </h:panelGroup>
            </h:form>
        </rich:popupPanel>
    </ui:define>
</ui:composition>

</html>
